package com.sensoro.study.httpclient;

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class HttpStudy {

    public static void main(String[] args) throws IOException, InterruptedException {

        HttpClient httpClient = HttpClient.newBuilder()
                .build();

        HttpRequest httpRequest = HttpRequest.newBuilder()
                .GET()
                .uri(URI.create("https://lins-test1-api.sensoro.com/petition/v1/petition/tasks/1750001149703421954/visual/channels"))
                .header("Authorization", "Bearer eyJhbGciOiJIUzUxMiJ9.eyJhY2NvdW50SWQiOiIxNDI3NDY5MDcyOTEzMjkzMzE0IiwiYXZhdGFySWQiOiIxNjkyNDE5MzUyMjIyNjcwODQ5IiwibWVyY2hhbnRJZCI6IjE2MjI5MDM1NDI3Mjg0NzA1MjkiLCJuaWNrbmFtZSI6IuW8oOaXpeazoiIsInRlbmFudElkIjoiMTYyMjkwMzU0MjYyMzYxMjkzMCIsImV4cCI6MTcwNjg0NDkxMCwidXNlcklkIjoiMTY5MjQxOTM1MjIyMjY3MDg0OSIsImlhdCI6MTcwNjI0MDExMCwidXNlcm5hbWUiOiIrODYxNTIwMTYxMTAzNyJ9.L60O9tZRqj_sE_-ZXUWdNmCyFwUC3bicMg22obZ752ou05ThjjV1-y4nNwbSR_h0PrNNnhlrqxZqsJDLIkTQDA")
                .header("X-Lins-Projectid", "1622903550156582913")
                .header("X-Lins-Tenantid", "1622903542623612930")
                .build();

        // 同步执行方式
        HttpResponse<String> responseBody = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());
        System.out.println(responseBody.body());

        // 异步执行方式
        httpClient.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString())
                .thenApply(HttpResponse::body)
                .thenAccept(System.out::println)
                .join();
    }
}
